﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Save_Editor
{
    /// <summary>
    /// Interaction logic for DateTimePicker.xaml
    /// </summary>
    public partial class DateTimePicker : UserControl
    {
        public static readonly DependencyProperty pHour = DependencyProperty.Register("Hour", typeof(int), typeof(DateTimePicker),
            new UIPropertyMetadata(0, new PropertyChangedCallback(TimeChanged)));
        public static readonly DependencyProperty pMinute = DependencyProperty.Register("Minute", typeof(int), typeof(DateTimePicker),
            new UIPropertyMetadata(0, new PropertyChangedCallback(TimeChanged)));
        public static readonly DependencyProperty pSecond = DependencyProperty.Register("Second", typeof(int), typeof(DateTimePicker),
            new UIPropertyMetadata(0, new PropertyChangedCallback(TimeChanged)));
        public static readonly DependencyProperty pTime = DependencyProperty.Register("Time", typeof(TimeSpan), typeof(DateTimePicker),
            new UIPropertyMetadata(TimeSpan.Zero, new PropertyChangedCallback(TimeChanged)));
        public static readonly DependencyProperty pMax = DependencyProperty.Register("Max", typeof(DateTime?), typeof(DateTimePicker),
            new UIPropertyMetadata(null, new PropertyChangedCallback(LimitChanged)));
        public static readonly DependencyProperty pMin = DependencyProperty.Register("Min", typeof(DateTime?), typeof(DateTimePicker),
            new UIPropertyMetadata(null, new PropertyChangedCallback(LimitChanged)));
        public static readonly DependencyProperty pAMPM = DependencyProperty.Register("AMPM", typeof(int), typeof(DateTimePicker),
            new UIPropertyMetadata(0, new PropertyChangedCallback(LimitChanged)));

        public DateTimePicker()
        {
            InitializeComponent();
            if (date.DisplayDateStart.HasValue)
                date.DisplayDate = date.DisplayDateStart.Value;
        }

        private void text_KeyDown(object sender, KeyEventArgs e)
        {
            KeyConverter kc = new KeyConverter();
            int i = 0;
            e.Handled = !int.TryParse(kc.ConvertToString(e.Key).Replace("NumPad", ""), out i);
        }

        private static void TimeChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            if (e.Property.Name == "Hour")
            {
                if ((int)e.NewValue > 12)
                {
                    int newHr =  ((int)e.NewValue % 12);
                    newHr += newHr == 0 ? 1 : 0;
                    obj.SetValue(e.Property, newHr);
                    obj.SetValue(pAMPM, 1);
                }
                else
                    obj.SetValue(pAMPM, 0);
            }
        }

        private static void LimitChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {

        }

        public int Hour
        {
            get { return (int)GetValue(pHour); }
            set { SetValue(pHour, value); }
        }

        public int Minute
        {
            get { return (int)GetValue(pMinute); }
            set { SetValue(pMinute, value); }
        }

        public int Second
        {
            get { return (int)GetValue(pSecond); }
            set { SetValue(pSecond, value); }
        }

        public TimeSpan Time
        {
            get
            {
                TimeSpan time = new TimeSpan(Hour, Minute, Second);
                if (AMPM == 1)
                    time = time.Add(TimeSpan.FromDays(0.5));
                if (time.CompareTo(TimeSpan.FromDays(1)) >= 1)
                {
                    time = TimeSpan.FromDays(1).Subtract(TimeSpan.FromMinutes(1));
                    Time = time;
                }
                return time;
            }
            set
            {
                if (value.CompareTo(TimeSpan.FromDays(1)) >= 1)
                {
                    value = value.Subtract(TimeSpan.FromMinutes(1));
                }
                Hour = value.Hours;
                Minute = value.Minutes;
                Second = value.Seconds;
            }
        }

        public DateTime Value
        {
            get
            {
                if (!date.SelectedDate.HasValue)
                    return DateTime.Now;
                else
                {
                    DateTime ret = date.SelectedDate.Value.AddTicks(Time.Ticks);
                    if (Max.HasValue && ret.CompareTo(Max.Value) >= 1)
                        ret = Max.Value;
                    if (Min.HasValue && ret.CompareTo(Min.Value) <= 0)
                        ret = Min.Value;
                    Value = ret;
                    return ret;
                }
            }
            set
            {
                if (Max.HasValue && value.CompareTo(Max.Value) >= 1)
                    value = Max.Value;
                if (Min.HasValue && value.CompareTo(Min.Value) <= 0)
                    value = Min.Value;
                date.SelectedDate = value.Date;
                Time = value.TimeOfDay;
            }
        }

        public DateTime? Max
        {
            get { return (DateTime?)GetValue(pMax); }
            set
            {
                SetValue(pMax, value);
            }
        }

        public DateTime? Min
        {
            get { return (DateTime?)GetValue(pMin); }
            set
            {
                SetValue(pMin, value);
            }
        }

        public int AMPM
        {
            get { return (int)GetValue(pAMPM); }
            set { SetValue(pAMPM, value); }
        }
    }
}
